home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Extensions / img / imgformatmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  9.5 KB  |  304 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /*
  26. ** Currently you cannot do anything interesting with the imgformat
  27. ** objects (except use them in object-identity comparisons).
  28. **
  29. ** Eventually, you should be able to ask things like type (rgb, cmap),
  30. ** bits/pixel, pixel alignment, row alignment, direction, etc.
  31. **
  32. */
  33.  
  34. /* Imgformat objects */
  35.  
  36. #include "Python.h"
  37.  
  38. typedef struct {
  39.     PyObject_HEAD
  40.     char    *name;
  41.     PyObject    *descr;
  42. } imgformatobject;
  43.  
  44. staticforward PyTypeObject Imgformattype;
  45.  
  46. #define is_imgformatobject(v)        ((v)->ob_type == &Imgformattype)
  47.  
  48. char doc_format[] =
  49.   "An image-format object completely identifies the way image-data\n"
  50.   "is stored in-core. The 'name' attribute has a textual name, the\n"
  51.   "'descr' attribute is a dictionary listing various attributes\n";
  52.  
  53. static PyObject *dict;            /* Dictionary of known types */
  54.  
  55. static imgformatobject *
  56. newimgformatobject(name, descr)
  57.     char *name;
  58.         PyObject *descr;
  59. {
  60.     imgformatobject *xp;
  61.     xp = PyObject_NEW(imgformatobject, &Imgformattype);
  62.     if (xp == NULL)
  63.         return NULL;
  64.     if( (xp->name = malloc(strlen(name)+1)) == NULL ) {
  65.         PyMem_DEL(xp);
  66.         return (imgformatobject *)PyErr_NoMemory();
  67.     }
  68.     strcpy(xp->name, name);
  69.     xp->descr = descr;
  70.     return xp;
  71. }
  72.  
  73. /* Imgformat methods */
  74.  
  75. static void
  76. imgformat_dealloc(xp)
  77.     imgformatobject *xp;
  78. {
  79.     free(xp->name);
  80.     PyMem_DEL(xp);
  81. }
  82.  
  83. static PyObject *
  84. imgformat_repr(self)
  85.     imgformatobject *self;
  86. {
  87.     char buf[100];
  88.  
  89.     sprintf(buf, "<imgformat '%.70s' at %x>", self->name, self);
  90.     return PyString_FromString(buf);
  91. }
  92.  
  93. static struct PyMethodDef imgformat_methods[] = {
  94.     {NULL,        NULL}        /* sentinel */
  95. };
  96.  
  97. static PyObject *
  98. imgformat_getattr(self, attr)
  99.     imgformatobject *self;
  100.         char *attr;
  101. {
  102.     if ( strcmp(attr, "name") == 0 )
  103.         return PyString_FromString(self->name);
  104.     if ( strcmp(attr, "__doc__") == 0 )
  105.         return PyString_FromString(doc_format);
  106.     if ( strcmp(attr, "descr") == 0 ) {
  107.         Py_INCREF(self->descr);
  108.         return self->descr;
  109.     }
  110.     return Py_FindMethod(imgformat_methods, (PyObject *)self, attr);
  111. }
  112.  
  113. static PyTypeObject Imgformattype = {
  114.     PyObject_HEAD_INIT(&PyType_Type)
  115.     0,            /*ob_size*/
  116.     "imgformat",        /*tp_name*/
  117.     sizeof(imgformatobject), /*tp_basicsize*/
  118.     0,            /*tp_itemsize*/
  119.     /* methods */
  120.     (destructor)imgformat_dealloc, /*tp_dealloc*/
  121.     0,            /*tp_print*/
  122.     (getattrfunc)imgformat_getattr,     /*tp_getattr*/
  123.     0,             /*tp_setattr*/
  124.     0,            /*tp_compare*/
  125.     (reprfunc)imgformat_repr, /*tp_repr*/
  126.     0,            /*tp_as_number*/
  127.     0,            /*tp_as_sequence*/
  128.     0,            /*tp_as_mapping*/
  129.     0,            /*tp_hash*/
  130. };
  131.  
  132. static char doc_new[] =
  133.  "Create a new format object. Arguments are name, description_text and\n"
  134.  "an optional attribute dictionary\n";
  135.  
  136. static PyObject *
  137. imgformat_new(self, args)
  138.     PyObject *self; /* Not used */
  139.     PyObject *args;
  140. {
  141.     char *name, *descrtext;
  142.     PyObject *descrobj;
  143.     PyObject *obj;
  144.     
  145.     descrobj = NULL;
  146.     if (!PyArg_ParseTuple(args, "ss|O", &name, &descrtext, &descrobj))
  147.         return NULL;
  148.     if ( !descrobj )
  149.         descrobj = Py_None;
  150.     Py_INCREF(descrobj);
  151.     obj = (PyObject *)newimgformatobject(descrtext, descrobj);
  152.     if (obj == NULL)
  153.         return NULL;
  154.     if (PyDict_SetItemString(dict, name, obj) != 0) {
  155.         Py_DECREF(obj);
  156.         Py_DECREF(descrobj);
  157.         return NULL;
  158.     }
  159.     return obj;
  160. }
  161.  
  162. /* Helper function for other modules, to obtain imgformat-by-name */
  163. PyObject *
  164. getimgformat(name)
  165.     char *name;
  166. {
  167.     return PyDict_GetItemString(dict, name);
  168. }
  169.  
  170. /* List of functions defined in the module */
  171.  
  172. static struct PyMethodDef imgformat_module_methods[] = {
  173.     {"new",        imgformat_new,    1,    doc_new},
  174.     {NULL,        NULL}        /* sentinel */
  175. };
  176.  
  177.  
  178. /* Initialization function for the module (*must* be called initimgformat) */
  179. static char doc_imgformat[] = "Container module for known image formats";
  180.  
  181. void
  182. initimgformat()
  183. {
  184.     PyObject *m, *x, *o;
  185.  
  186.     /* Create the module and add the functions */
  187.     m = Py_InitModule("imgformat", imgformat_module_methods);
  188.  
  189.     dict = PyModule_GetDict(m);
  190.     x = PyString_FromString(doc_imgformat);
  191.     PyDict_SetItemString(dict, "__doc__", x);
  192.  
  193.     /* 32 bit RGB pixels with R in low-order bits */
  194.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii)(ii))}", "type", "rgb",
  195.             "b2t", 0, "size", 32, "align", 32,
  196.             "comp", 0, 8, 8, 8, 16, 8, 24, 8);
  197.     x = (PyObject *)newimgformatobject("SGI 32bit RGB(A)", o);
  198.     PyDict_SetItemString(dict, "rgb", x);
  199.  
  200.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii)(ii))}", "type", "rgb",
  201.             "b2t", 1, "size", 32, "align", 32,
  202.             "comp", 0, 8, 8, 8, 16, 8, 24, 8);
  203.     x = (PyObject *)newimgformatobject("SGI 32bit RGB(A) bottom-to-top", o);
  204.     PyDict_SetItemString(dict, "rgb_b2t", x);
  205.  
  206.     /* 32 bit RGB pixels with B in low-order bits */
  207.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii))}", "type", "rgb",
  208.             "b2t", 0, "size", 32, "align", 32,
  209.             "comp", 16, 8, 8, 8, 0, 8);
  210.     x = (PyObject *)newimgformatobject("Macintosh 32bit RGB", o);
  211.     PyDict_SetItemString(dict, "macrgb", x);
  212.  
  213.     /* 16 bit RGB pixels with B in low-order bits */
  214.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii))}", "type", "rgb",
  215.             "b2t", 0, "size", 16, "align", 16,
  216.             "comp", 10, 5, 5, 5, 0, 5);
  217.     x = (PyObject *)newimgformatobject("Macintosh 16bit RGB", o);
  218.     PyDict_SetItemString(dict, "macrgb16", x);
  219.  
  220.     /* 8 bit RRRBBGGG pixels, with 32bit aligned rows */
  221.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii))}", "type", "rgb",
  222.             "b2t", 0, "size", 8, "align", 32,
  223.             "comp", 5, 3, 0, 3, 3, 2);
  224.     x = (PyObject *)newimgformatobject("SGI 3:3:2 RGB", o);
  225.     PyDict_SetItemString(dict, "rgb8", x);
  226.  
  227.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii))}", "type", "rgb",
  228.             "b2t", 1, "size", 8, "align", 32,
  229.             "comp", 5, 3, 0, 3, 3, 2);
  230.     x = (PyObject *)newimgformatobject("SGI 3:3:2 RGB bottom-to-top", o);
  231.     PyDict_SetItemString(dict, "rgb8_b2t", x);
  232.  
  233.     /* 8 bit greyscale pixels with 32bit aligned rows */
  234.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "grey",
  235.             "b2t", 0, "size", 8, "align", 32,
  236.             "comp", 0, 8);
  237.     x = (PyObject *)newimgformatobject("SGI 8bit grey", o);
  238.     PyDict_SetItemString(dict, "grey", x);
  239.  
  240.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "grey",
  241.             "b2t", 1, "size", 8, "align", 32,
  242.             "comp", 0, 8);
  243.     x = (PyObject *)newimgformatobject("SGI 8bit grey bottom-to-top", o);
  244.     PyDict_SetItemString(dict, "grey_b2t", x);
  245.  
  246.     /* 8bit colormap pixels with 32bit aligned rows */
  247.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "mapped",
  248.             "b2t", 0, "size", 8, "align", 32,
  249.             "comp", 0, 8);
  250.     x = (PyObject *)newimgformatobject("SGI 8bit colormap", o);
  251.     PyDict_SetItemString(dict, "colormap", x);
  252.  
  253.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "mapped",
  254.             "b2t", 1, "size", 8, "align", 32,
  255.             "comp", 0, 8);
  256.     x = (PyObject *)newimgformatobject("SGI 8bit colormap bottom-to-top", o);
  257.     PyDict_SetItemString(dict, "colormap_b2t", x);
  258.  
  259.     /* 8 bit RRRBBGGG pixels, without alignment */
  260.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii))}", "type", "rgb",
  261.             "b2t", 0, "size", 8, "align", 8,
  262.             "comp", 5, 3, 0, 3, 3, 2);
  263.     x = (PyObject *)newimgformatobject("X 3:3:2 RGB", o);
  264.     PyDict_SetItemString(dict, "xrgb8", x);
  265.  
  266.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii)(ii)(ii))}", "type", "rgb",
  267.             "b2t", 1, "size", 8, "align", 8,
  268.             "comp", 5, 3, 0, 3, 3, 2);
  269.     x = (PyObject *)newimgformatobject("X 3:3:2 RGB bottom-to-top", o);
  270.     PyDict_SetItemString(dict, "xrgb8_b2t", x);
  271.  
  272.     /* 8 bit greyscale pixels without alignment */
  273.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "grey",
  274.             "b2t", 0, "size", 8, "align", 8,
  275.             "comp", 0, 8);
  276.     x = (PyObject *)newimgformatobject("X 8bit grey", o);
  277.     PyDict_SetItemString(dict, "xgrey", x);
  278.  
  279.     /* 8 bit greyscale pixels without alignment */
  280.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "grey",
  281.             "b2t", 1, "size", 8, "align", 8,
  282.             "comp", 0, 8);
  283.     x = (PyObject *)newimgformatobject("X 8bit grey bottom-to-top", o);
  284.     PyDict_SetItemString(dict, "xgrey_b2t", x);
  285.  
  286.     /* 8bit colormap pixels without alignment */
  287.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "mapped",
  288.             "b2t", 0, "size", 8, "align", 8,
  289.             "comp", 0, 8);
  290.     x = (PyObject *)newimgformatobject("X 8bit colormap", o);
  291.     PyDict_SetItemString(dict, "xcolormap", x);
  292.  
  293.     /* bitmap stored as a pixel per byte in low-order bit */
  294.     o = Py_BuildValue("{s:s,s:i,s:i,s:i,s:((ii))}", "type", "grey",
  295.             "b2t", 0, "size", 8, "align", 8,
  296.             "comp", 0, 1);
  297.     x = (PyObject *)newimgformatobject("pbm-style bitmap", o);
  298.     PyDict_SetItemString(dict, "pbmbitmap", x);
  299.  
  300.     /* Check for errors */
  301.     if (PyErr_Occurred())
  302.         Py_FatalError("can't initialize module imgformat");
  303. }
  304.